package com.bree.erp.entity;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@NamedQueries({
	@NamedQuery(name = ReturnsBalance.QUERY_SELECT_ALL_BALANCE, query = "SELECT b FROM ReturnsBalance b"),
	@NamedQuery(name = ReturnsBalance.QUERY_SUM_ALL_BALANCE, query = "SELECT new com.bree.erp.entity.ReturnsBalance(b.partUnit, SUM(b.totalBalance)) FROM ReturnsBalance b GROUP BY b.partUnit")
	}
)
@Entity
@IdClass(BalanceId.class)
public class ReturnsBalance implements CrudId<BalanceId> {
	
	public static final String QUERY_SELECT_ALL_BALANCE = "findAllBalance";
	public static final String QUERY_SUM_ALL_BALANCE = "sumAllBalance";
	
	public ReturnsBalance() {
		
	}
	
	public ReturnsBalance(PartUnit partUnit, double totalBalance) {
		this.partUnit = partUnit;
		this.totalBalance = totalBalance;
	}
	
	@Transient
	private BalanceId id;
	
	@Id
	@OneToOne
	private Warehouse warehouse;

	@Id
	@OneToOne
	private PartUnit partUnit;
	
	private double totalBalance;

	@Temporal(TemporalType.TIMESTAMP)
	private Date lastMovement;


	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the partUnit
	 */
	public PartUnit getPartUnit() {
		return partUnit;
	}

	/**
	 * @param partUnit the partUnit to set
	 */
	public void setPartUnit(PartUnit partUnit) {
		this.partUnit = partUnit;
	}

	/**
	 * @return the totalBalance
	 */
	public double getTotalBalance() {
		return totalBalance;
	}

	/**
	 * @param totalBalance the totalBalance to set
	 */
	public void setTotalBalance(double totalBalance) {
		this.totalBalance = totalBalance;
	}

	/**
	 * @return the lastMovement
	 */
	public Date getLastMovement() {
		return lastMovement;
	}

	/**
	 * @param lastMovement the lastMovement to set
	 */
	public void setLastMovement(Date lastMovement) {
		this.lastMovement = lastMovement;
	}

	@Override
	public BalanceId getId() {
		
		if(id == null) {
			id = new BalanceId(warehouse.getId(), partUnit.getId());
		}
		
		return id;
	}
}
